<?php

namespace app\studentLayer\model;


use think\Db;
use think\Exception;

class TeacherModel
{

    public static function SelectTeacherModel()
    {
        /*
                select t.*,GROUP_CONCAT(c.name) as c_name from s_teacher as t
                left join s_teaching as tc on t.id = tc.t_id
                left join s_course as c on c.id = tc.c_id
                group by t.id
                */
        $result = Db::table('s_teacher')->alias('t')->field('t.*,group_concat(c.name) as c_name')
            ->leftJoin('s_teaching tc', 't.id=tc.t_id')
            ->leftJoin('s_course c', 'c.id = tc.c_id')
            ->group('t.id');
           // ->select();

        return $result;
    }


    public static function InsertTeacherModel(array $insert)
    {
        //开启事务，因为对于老师表的添加不单单是添加一个老师表，同时还要添加与其相关的科目关联表，也就是师科表
        Db::startTrans();
        try {
            //var_dump($insert);die;
            $teaching['c_id'] = $insert['c_name'];
            unset($insert['c_name']);

            //获取添加的id，是先添加然后在返回id
            $insertGetId = Db::table('s_teacher')->insertGetId($insert);

            //获取添加的最后一条id，直接获取的
            //$insertGetId = Db::table('s_course')->getLastInsID();
            $teaching['t_id'] = $insertGetId;
             //var_dump($teaching);die;


            /*            用来测试foreach() 里面的数字的
                        foreach($teaching['c_id'] as $value){

                        }
                        var_dump($value);die;
            */
            //方法跟以前写的略有不同，但是效果一样，可以结合起来看看



            if (isset($teaching['t_id']) && $teaching['t_id']) {


                    foreach ($teaching['c_id'] as $value) {
                        //var_dump($value);
                        $result = Db::table('s_teaching')->insert(['t_id' => $teaching['t_id'], 'c_id' => $value]);
                    }
//var_dump($result);die;
                if ($result) {
                    //提交
                    Db::commit();
                    return $result;

                } else {
                    //回滚
                    Db::rollback();
                }

            } else {
                //回滚
                Db::rollback();
            }

        } catch (Exception $exception) {
            //回滚
            Db::rollback();
        }

        //防止一直回滚，占用系统资源
        return null;
    }

    public static function UpdateTeacherModel(array $update)
    {

        //var_dump($update);die;

        //开启事务，因为对于老师表的添加不单单是添加一个老师表，同时还要添加与其相关的科目关联表，也就是师科表
        Db::startTrans();
        try {
            //师生表的两个id
            $teaching['t_id'] = $update['id'];
            $teaching['c_id'] = $update['c_name'];
            unset($update['c_name']);

            //var_dump($teaching);die;
            $start = Db::table('s_teacher')->where('id', $update["id"])->update($update);

            if(isset($teaching) && $teaching) {

                //不同于添加，这里进行修改，因为一个老师关联者多个科目，如果修改，进行判断的话，最后所有记录全部会被覆盖，变成最后修改的一条记录
                //必须先删除原本的记录在进行添加记录
              // var_dump($teaching);die;
                $DeleteTeaching = Db::table('s_teaching')->where('t_id', $teaching['t_id'])->delete();
                //var_dump($DeleteTeaching);die;
                if ($DeleteTeaching) {

                    foreach ($teaching['c_id'] as $value) {
                        $result = Db::table('s_teaching')->insert(['t_id' => $teaching['t_id'], 'c_id' => $value]);
                    }

                    if ($result) {
                        //提交
                        Db::commit();
                        return $result;

                    } else {
                        //回滚
                        Db::rollback();
                    }

                } else {
                    Db::rollback();
                }

            }

        } catch (Exception $exception) {
            //回滚
            Db::rollback();
        }

        //防止一直回滚，占用系统资源
        return null;
    }

    public static function DeleteTeacher(array $delete){

        //开启事务
        Db::startTrans();
        try {

            $DeleteTeacher = Db::table('s_teacher')->where($delete)->delete();
            // var_dump($DeleteTeaching);die;
            if ($DeleteTeacher) {

                $DeleteTeaching = Db::table('s_teaching')->where('t_id', $delete['id'])->delete();
                //var_dump(1);die;
                if ($DeleteTeaching) {
                    //提交
                    Db::commit();
                    return $DeleteTeaching;

                } else {
                    //回滚
                    Db::rollback();
                }

            } else {
                Db::rollback();
            }
        }catch(Exception $exception){

            Db::rollback();
        }



    }










}











